<template>
  <div class="resource-pricing">
    <div class="pricing-header">
      <h4>计价策略</h4>
      <a-button type="primary" size="small" @click="handleAddPricing">
        <IconifyIcon icon="mdi:plus" />
        添加策略
      </a-button>
    </div>

    <div class="pricing-content">
      <a-table
        :data-source="pricingRules"
        :columns="pricingColumns"
        :pagination="false"
        size="small"
        bordered
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'pricing_type'">
            <a-tag :color="getPricingTypeColor(record.pricing_type)">
              {{ getPricingTypeText(record.pricing_type) }}
            </a-tag>
          </template>
          <template v-else-if="column.key === 'perspective'">
            <a-tag :color="getPerspectiveColor(record.perspective)">
              {{ getPerspectiveText(record.perspective) }}
            </a-tag>
          </template>
          <template v-else-if="column.key === 'status'">
            <a-tag :color="getStatusColor(record.status)">
              {{ getStatusText(record.status) }}
            </a-tag>
          </template>
          <template v-else-if="column.key === 'action'">
            <a-space>
              <a-button type="link" size="small" @click="handleEdit(record)">
                <IconifyIcon icon="mdi:pencil" />
                编辑
              </a-button>
              <a-button type="link" size="small" @click="handleDelete(record)">
                <IconifyIcon icon="mdi:delete" />
                删除
              </a-button>
            </a-space>
          </template>
        </template>
      </a-table>

      <div v-if="pricingRules.length === 0" class="empty-state">
        <a-empty description="暂无计价策略" />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { IconifyIcon } from '@vben/icons';
import type { ResourceItem } from '#/api/resource-center';

interface Props {
  resource: ResourceItem | null;
}

const props = defineProps<Props>();

// 模拟计价规则数据
const pricingRules = ref([
  {
    id: '1',
    name: '标准计费',
    pricing_type: 'PER_DAY',
    amount: 100,
    currency: 'CNY',
    min_quantity: 1,
    max_quantity: 10,
    perspective: 'CUSTOMER',
    status: 'ACTIVE',
    effective_time: '2024-01-01 00:00:00',
    expire_time: '2024-12-31 23:59:59',
  },
  {
    id: '2',
    name: 'VIP优惠',
    pricing_type: 'PACKAGE',
    amount: 800,
    currency: 'CNY',
    min_quantity: 1,
    max_quantity: 5,
    perspective: 'PLATFORM',
    status: 'ACTIVE',
    effective_time: '2024-01-01 00:00:00',
    expire_time: '2024-12-31 23:59:59',
  },
]);

const pricingColumns = [
  { title: '策略名称', dataIndex: 'name', key: 'name', width: 120 },
  {
    title: '计费类型',
    dataIndex: 'pricing_type',
    key: 'pricing_type',
    width: 100,
  },
  { title: '金额', dataIndex: 'amount', key: 'amount', width: 80 },
  { title: '币种', dataIndex: 'currency', key: 'currency', width: 60 },
  { title: '视角', dataIndex: 'perspective', key: 'perspective', width: 80 },
  { title: '状态', dataIndex: 'status', key: 'status', width: 80 },
  { title: '操作', key: 'action', width: 120 },
];

const getPricingTypeColor = (type: string) => {
  switch (type) {
    case 'PER_DAY':
      return 'blue';
    case 'PER_HOUR':
      return 'green';
    case 'ONE_TIME':
      return 'orange';
    case 'PACKAGE':
      return 'purple';
    case 'SUBSCRIPTION':
      return 'cyan';
    default:
      return 'default';
  }
};

const getPricingTypeText = (type: string) => {
  switch (type) {
    case 'PER_DAY':
      return '按天';
    case 'PER_HOUR':
      return '按小时';
    case 'ONE_TIME':
      return '一次性';
    case 'PACKAGE':
      return '套餐';
    case 'SUBSCRIPTION':
      return '订阅';
    default:
      return '未知';
  }
};

const getPerspectiveColor = (perspective: string) => {
  switch (perspective) {
    case 'CUSTOMER':
      return 'blue';
    case 'PLATFORM':
      return 'green';
    default:
      return 'default';
  }
};

const getPerspectiveText = (perspective: string) => {
  switch (perspective) {
    case 'CUSTOMER':
      return '客户视角';
    case 'PLATFORM':
      return '平台视角';
    default:
      return '未知';
  }
};

const getStatusColor = (status: string) => {
  switch (status) {
    case 'ACTIVE':
      return 'success';
    case 'INACTIVE':
      return 'error';
    case 'EXPIRED':
      return 'warning';
    default:
      return 'default';
  }
};

const getStatusText = (status: string) => {
  switch (status) {
    case 'ACTIVE':
      return '生效';
    case 'INACTIVE':
      return '停用';
    case 'EXPIRED':
      return '过期';
    default:
      return '未知';
  }
};

const handleAddPricing = () => {
  console.log('添加计价策略');
};

const handleEdit = (record: any) => {
  console.log('编辑计价策略', record);
};

const handleDelete = (record: any) => {
  console.log('删除计价策略', record);
};
</script>

<style lang="less" scoped>
.resource-pricing {
  .pricing-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;

    h4 {
      margin: 0;
      font-size: 16px;
      font-weight: 600;
    }
  }

  .pricing-content {
    .empty-state {
      padding: 40px 0;
      text-align: center;
    }
  }
}
</style>
